package com.aprointeractive.android.justwar.gcm.service;

import org.json.JSONException;
import org.json.JSONObject;

import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.aprointeractive.android.justwar.activity.JustwarActivity;
import com.aprointeractive.android.justwar.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

	public static final String ME = "GCMReceiver";

	public GCMIntentService() {
		super("GCMIntentService");
	}

	private static final String TAG = "GCMIntentService";

	@Override
	public void onRegistered(Context context, String regId) {

		Log.v(ME + ":onRegistered", "Registration ID arrived!");
		Log.v(ME + ":onRegistered", regId);

		JSONObject json;

		try {
			json = new JSONObject().put("event", "registered");
			json.put("regid", regId);
			json.put("udid", JustwarActivity.UDID);

			Log.v(ME + ":onRegisterd", json.toString());

			// Send this JSON data to the JavaScript application above EVENT
			// should be set to the msg type
			// In this case this is the registration ID
			JustwarActivity.sendJavascript(json, "GCM_Event");

		} catch (JSONException e) {
			// No message to the user is sent, JSON failed
			Log.e(ME + ":onRegisterd", "JSON exception");
		}
	}

	@Override
	public void onUnregistered(Context context, String regId) {
		Log.d(TAG, "onUnregistered - regId: " + regId);
	}

	@Override
	protected void onMessage(Context context, Intent intent) {
		Log.d(TAG, "onMessage - context: " + context);

		// Extract the payload from the message
		Bundle extras = intent.getExtras();
		if (extras != null) {
			try {
				Log.v(ME + ":onMessage extras ", extras.getString("message"));

				// First Key-Value is 'event / pushmessage'
				// The 'event' key use in common_android.js's GCM_Event function
				// After the event key, you can customize your message
				// Example Key-Value is 'pushmessage / extras.pushmessage'
				// Extras is intent from push message server
				JSONObject json;
				json = new JSONObject().put("event", "message");// 이벤트 콜백
																// 함수(GCM_Event)가
				json.put("message", extras.getString("message"));

				Log.v(ME + ":onMessage ", json.toString());

				JustwarActivity.sendJavascript(json, "GCM_Event");
				
				generateNotification(context, extras.getString("message"));
				// Send the MESSAGE to the Javascript application
			} catch (JSONException e) {
				Log.e(ME + ":onMessage", "JSON exception");
			}
		}

	}

	@Override
	public void onError(Context context, String errorId) {
		Log.e(TAG, "onError - errorId: " + errorId);
	}

	private static void generateNotification(Context context, String message) {
		try {
			String title = "JustWar";
			Notification notif = new Notification(
					android.R.drawable.btn_star_big_on, message,
					System.currentTimeMillis());
			notif.flags = Notification.FLAG_AUTO_CANCEL;
			notif.defaults = Notification.DEFAULT_SOUND;
			notif.defaults = Notification.DEFAULT_VIBRATE;

			Intent notificationIntent = new Intent(context,
					JustwarActivity.class);
			notificationIntent.putExtra("message", message);
			notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
			PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
					notificationIntent, 0);

			notif.setLatestEventInfo(context, title, message, contentIntent);
			String ns = Context.NOTIFICATION_SERVICE;
			NotificationManager mNotificationManager = (NotificationManager) context
					.getSystemService(ns);
			mNotificationManager.notify(1, notif);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
